$("#isShow").hide();
var stepId = $CertusJsonAjax.getStepInstanceId();
var processId = $CertusJsonAjax.getProInstanceId();
$CertusFormAjax.ajax("findSpUserList",{},function(response){
    $('#selectPage').bSelectPage({
        showField : 'userName',
        keyField : 'userId',
        data : response.data,
        multiple : true
    });
});

$CertusFormAjax.ajax('initDepts',{},function (response) {
    var data = response.data;
    $$scope.depts = data;
    if (data.length==1){
        $("#deptCode").attr("disabled","disabled");
    }
});

this.hdoabtmeg = function () {
    $("#isShow").hide();
    $$scope.isEdit = 0;
}
$CertusFormAjax.ajax("findSeProList",{},function(response){
    $$scope.proList = response.data;
});

// 出差计划
var oabtColumns = [{
    id: 'projName',
    title: '归属项目',
    width: 150,
    type: 'string',
    columnClass: 'text-center',
}, {
    id: 'planStarTime',
    title: '计划开始时间',
    width: 100,
    type: 'date',
    format:'yyyy-MM-dd hh:mm:ss',
    otype:'time_stamp_ms',
    columnClass: 'text-center',
}, {
    id: 'planEndTime',
    title: '计划结束时间',
    width: 100,
    type: 'date',
    format:'yyyy-MM-dd hh:mm:ss',
    otype:'time_stamp_ms',
    columnClass: 'text-center',
}, {
    id: 'planCity',
    title: '地点',
    width: 100,
    type: 'string',
    columnClass: 'text-center',
}, {
    id: 'planComments',
    title: '工作计划',
    type: 'string',
    columnClass: 'text-left',
}, {
    id: 'operation',
    title: '操作',
    width: 130,
    type: 'string',
    columnClass: 'text-center',
    resolution: function (value, record, column, grid, dataNo, columnNo) {
        var content = '';
        content += '<button class="btn btn-xs btn-info" onclick="editPlans('+record.id+');"><i class="fa fa-edit"></i>  编辑</button>';
        return content;
    }
}];
$CertusFormAjax.ajax("findPlansList",{"processId":processId},function(response){
    var data = response.data;
    var oabtOption = {
        lang: 'zh-cn',
        ajaxLoad: false,
        extraWidth: 20,
        // loadURL:'url',
        exportFileName: '出差计划',
        datas:data,
        columns: oabtColumns,
        gridContainer: 'oabtContainer1',
        tools: ''
    };
    var oabtGrid= $.fn.dlshouwen.grid.init(oabtOption);
    oabtGrid.load();
    $$scope.id =data[0].id;
});

this.changeType = function (value) {
    if (value==1){
        $("#isTrip").css("display","");
        $("#ist1").css("display","");
        $("#ist2").css("display","");
        $("#ist3").css("display","");
    }else {
        $("#isTrip").css("display","none");
        $("#ist1").css("display","none");
        $("#ist2").css("display","none");
        $("#ist3").css("display","none");
    }
};

function editPlans() {
    $("#isShow").show();
    $$scope.isEdit = 1;
    $CertusFormAjax.ajax('initDepts',{},function (response) {
        var data = response.data;
        $$scope.depts = data;

        $CertusFormAjax.ajax('findPlanInfo',{"processId":processId},function (response) {
            var data = response.data;
            var plans = data.plans;
            var bugetList = data.bugetList;
            var tripUsers = data.tripUsers;
            $$scope.stUserName = tripUsers.userName;
            $$scope.proId = plans.pro_id;
            $$scope.planCity = plans.plan_city;
            $$scope.planStarTime = plans.planStarTime;
            $$scope.planEndTime = plans.planEndTime;
            $$scope.planComments = plans.plan_comments;
            $$scope.id = plans.id;
            $$scope.deptCode = plans.dept_code;

            $("#selectPage").val(tripUsers.userId);
            $(".sp_element_box").first().append('<li class="selected_tag" ' +
                'itemvalue="'+data.userId+'">'+data.tripUsers.userName+'<span class="tag_close">x</span></li>');

            $$scope.type = plans.type+"";
            changeType(plans.type);
            for (var i=0;i<bugetList.length;i++){
                if (bugetList[i].bugetType=="火车/汽车"){
                    $$scope.carBuget = bugetList[i].buget;
                    $$scope.carId = bugetList[i].id;
                }else if (bugetList[i].bugetType=="飞机"){
                    $$scope.planeBuget = bugetList[i].buget;
                    $$scope.planeId = bugetList[i].id;
                }else if (bugetList[i].bugetType=="交通费用"){
                    $$scope.trafficBuget = bugetList[i].buget;
                    $$scope.trafficId = bugetList[i].id;
                }else if (bugetList[i].bugetType=="住宿费用"){
                    $$scope.stayBuget = bugetList[i].buget;
                    $$scope.stayId = bugetList[i].id;
                }else if (bugetList[i].bugetType=="招待费用"){
                    $$scope.serveBuget = bugetList[i].buget;
                    $$scope.serveId = bugetList[i].id;
                }else if (bugetList[i].bugetType=="其他费用"){
                    $$scope.otherBuget = bugetList[i].buget;
                    $$scope.otherId = bugetList[i].id;
                }else {
                    $$scope.subsidyBuget = bugetList[i].buget;
                    $$scope.subsidyId = bugetList[i].id;
                }
            }
        })
    });
}
//取消申请
$(".unagree").on('click',function () {
    $CertusFormAjax.ajax('qxTrips',{"id":$$scope.id,"stepId":stepId},function () {
        layer.msg("取消成功！");
        window.history.back();
    });
});

$(".submit").on('click',function () {
    var plansList=[];
    if ($$scope.isEdit==1){
        if (!certusValidateNotnull()){
            return;
        }
        var data = new Date();
        var datetime = $("#plansTime").val();
        var startTime = new Date(datetime.replace("/", "-").replace("/", "-"));
        if (datetime==""){
            layer.msg("计划开始日期不能为空",{time:1000});
            return;
        }
        var endTime = $("#planeTime").val();
        if (endTime==""){
            layer.msg("计划结束日期不能为空",{time:1000});
            return;
        }
        var endTimes = new Date(endTime.replace("/", "-").replace("/", "-"));
        if (startTime>endTimes){
            layer.msg("开始日期不能大于结束日期",{time:1000});
            return;
        }

        var planComments = $("#planComments").val();
        if (planComments.length>120){
            layer.msg("工作计划不能大于120字",{time:1000})
            return;
        }

        var planArr={};
        planArr.proName = $("#proId").find("option:selected").text();
        planArr.proId = $("#proId").val();
        planArr.planCity = $("#planCity").val();
        planArr.planStarTime = $("#plansTime").val();
        planArr.planEndTime = $("#planeTime").val();
        planArr.planComments = planComments;
        planArr.carBuget = $("#carBuget").val();
        planArr.planeBuget = $("#planeBuget").val();
        planArr.trafficBuget = $("#trafficBuget").val();
        planArr.stayBuget = $("#stayBuget").val();
        planArr.serveBuget = $("#serveBuget").val();
        planArr.otherBuget = $("#otherBuget").val();
        planArr.subsidyBuget = $("#subsidyBuget").val();
        planArr.carId = $$scope.carId;
        planArr.planeId = $$scope.planeId;
        planArr.trafficId = $$scope.trafficId;
        planArr.stayId = $$scope.stayId;
        planArr.serveId = $$scope.serveId;
        planArr.otherId = $$scope.otherId;
        planArr.subsidyId = $$scope.subsidyId;
        planArr.type = $("#type").val();
        if ($("#selectPage").val()==""){
            planArr.users=$$scope.stUser;
        }else{
            planArr.users=$("#selectPage").val();
        }
        planArr.deptCode = $("#deptCode").val();
        plansList.push(planArr);
    }
    var date = {};
    date.plansList = plansList;
    date.stepId= stepId;
    date.processId = processId;
    date.id =$$scope.id;
    // data.type = $("#type").val();
    $CertusFormAjax.ajax("editTrip",{"param":JSON.stringify(date),"lcCode":"NCCLC1"},function () {
        layer.msg("申请成功！");
        window.history.back();
    });
});

//个人出差申请end----
//显示出差计划表单
// function shoabtmeg() {
//     $('.hdoabtmeg').show();
// }
// // 隐藏出差计划表单
// function hdoabtmeg() {
//     $('.hdoabtmeg').hide();
// }

//项目其他费用表单显示隐藏
function hideother() {
    $(".hideother").hide();
};

function showother() {
    $(".hideother").show();
};


